<template>
  <div class='container'>
    <el-dialog
    :title="title"
    :visible="dialogVisible"
    width="30%"
    @close="closeDialog">
      <el-form ref="formRef" size="small" label-width="100px" :model="articleForm" :rules="rules">
        <el-form-item label="文章标题：" prop="title">
          <el-input v-model="articleForm.title" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <el-form-item label="文章内容：" prop="articleBody" >
          <quill-editor v-model="articleForm.articleBody" :options="editorOptions"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址：" prop="videoURL" class="videoBox" >
          <el-input v-model="articleForm.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitArticle">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles.js'
export default {
  name: 'articleAdd',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '新增文章'
    }
  },
  data () {
    return {
      articleForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', tirgger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', tirgger: 'blur' }
        ]
      },
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      }
    }
  },
  methods: {
    // 关闭对话框
    closeDialog () {
      this.$emit('update:dialogVisible', false)
      this.articleForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    // 提交文章
    submitArticle () {
      this.$refs.formRef.validate(async boo => {
        if (boo) {
          if (this.articleForm.id) {
            // 编辑模式
            await update(this.articleForm)
          } else {
            await add(this.articleForm)
          }
          this.closeDialog()
          this.$emit('reload')
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.quill-editor{
  height: 150px;
}
.videoBox{
  margin-top: 60px;
}
</style>
